/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.learn.learn_view.model_solution_view.command;


import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JOptionPane;

import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.learn.learn_controller.Controller;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.CmdButton;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.LearnInterface;
import br.ufmg.dees.insane.ui.rich.learn.learn_view.dialog.MatrixViewTableDialog;
import br.ufmg.dees.insane.util.SingularMatrixException;

/**
*
* @author Moreira, Renata & Pitangueira, Roque
* @since May 2005
*/
public class SolutionDialog {
	
	private static final long serialVersionUID = 1L;
	private LearnInterface parent;
	private FemModel fmodel;
	private MatrixViewTableDialog dlg;
	private double[][] disp;
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * 
	 */
	public SolutionDialog(LearnInterface parent) {
		this.parent = parent;
		fmodel = parent.getView().getController().getModel();
		
//		Number Format
		DecimalFormat fmt = new DecimalFormat();
        DecimalFormatSymbols fsym = new DecimalFormatSymbols();
        fsym.setDecimalSeparator('.');
        fmt.setDecimalFormatSymbols(fsym);
        fmt.applyPattern("0.000E00");
        
		
		try {
			((Controller)parent.getView().getController()).getDriver().getFemModel().cleanResults();
			long startTime = System.currentTimeMillis();
			((Controller)parent.getView().getController()).getDriver().analyseModel();
			long endTime = System.currentTimeMillis();
			long dif = (endTime - startTime);
			if (dif == 0) {
				parent.addOutputText("Model successfully analysed in 1 milisecond.");
			} else {
				parent.addOutputText("Model successfully analysed in " + dif + " miliseconds.");
			}
			fmodel.setHypostatic(false);
			
			 ArrayList sel = this.parent.getView().getController().getSelection();
		        //if ( sel.size() < 1 ) {
		        	//((Controller)parent.getView().getController()).selectAll();
		        	//sel = ((LearnController)parent.getView().getController()).getSelection();
				//}
				
				if (sel.size() == 0) {
					parent.addOutputText("There is no node in model.");
					return;
				}
		        
				int nLine = fmodel.getGlobalAnalysisModel().getNdf();
		        dlg = new MatrixViewTableDialog("Solution", nLine);
				ListIterator selLI = sel.listIterator();
				disp = new double[nLine][1];
				double pos;
				while(selLI.hasNext()){
					Node no = (Node)selLI.next();
					int cont = 0;
					for (int i=0; i<6; i++) {
						if (fmodel.getGlobalAnalysisModel().getValidEquation(i)) {
							pos = no.getDisplacement(i);
							disp[cont][0]= pos;
							cont++;
						};
					};
					CmdButton[][] dispVector = new CmdButton[nLine][1];
					for(int i =0; i< nLine; i++)
						for(int j =0; j<1; j++)
							dispVector[i][j]= new CmdButton(String.valueOf(fmt.format(disp[i][j])));
					
					String[] colNames = new String[1];
					for(int k=0; k<1; k++)
						colNames[k] = "Displacements";	
					dlg.addOutputTable("Node "+no.getLabel(),colNames,dispVector);
					
				}
				
				dlg.showDialog();
		}
		
		catch (SingularMatrixException e) {
			fmodel.setHypostatic(true);
			Toolkit.getDefaultToolkit().beep();
			JOptionPane.showMessageDialog(parent, "Hypostatic Structure.", "Hypostatic Structure", JOptionPane.ERROR_MESSAGE);
		}
		

        
       
		
    }
		
   

}
